#!/bin/bash

# 代练通后端部署脚本 (阿里云服务器版本)
# 只负责编译、上传和重启后端应用，不处理nginx配置

set -e  # 遇到错误立即退出

rm -rf kuangjia/target
# 配置变量
SERVER_HOST="root@47.118.21.120"
REMOTE_APP_DIR="/opt/dailiantong"
LOCAL_JAR_PATH="kuangjia/target/game-training-platform-1.0.0.jar"
SERVICE_NAME="dailiantong"

echo "🚀 开始部署代练通后端应用到阿里云服务器..."

# 1. 检查本地JAR文件
if [ ! -f "$LOCAL_JAR_PATH" ]; then
    echo "📦 未找到JAR包，开始编译..."
    cd kuangjia
    mvn clean package -DskipTests
    cd ..
    
    if [ ! -f "$LOCAL_JAR_PATH" ]; then
        echo "❌ 编译失败，未找到JAR包: $LOCAL_JAR_PATH"
        exit 1
    fi
fi

echo "✅ 找到JAR包: $LOCAL_JAR_PATH"

# 2. 停止远程服务
echo "🛑 停止远程应用服务..."
ssh $SERVER_HOST "systemctl stop $SERVICE_NAME" || true

# 3. 备份当前应用
echo "💾 备份当前应用..."
ssh $SERVER_HOST "
    if [ -f $REMOTE_APP_DIR/game-training-platform-1.0.0.jar ]; then
        cp $REMOTE_APP_DIR/game-training-platform-1.0.0.jar $REMOTE_APP_DIR/game-training-platform-1.0.0.jar.backup.\$(date +%Y%m%d_%H%M%S)
        echo '✅ 已备份当前应用'
    fi
"

# 4. 上传新应用
echo "📤 上传新的JAR包..."
scp "$LOCAL_JAR_PATH" "$SERVER_HOST:$REMOTE_APP_DIR/"

# 5. 上传证书文件 (如果不存在)
echo "📋 检查证书文件..."
ssh $SERVER_HOST "
    if [ ! -d $REMOTE_APP_DIR/cert ]; then
        mkdir -p $REMOTE_APP_DIR/cert
    fi
"

# 上传证书文件
if [ -d "kuangjia/src/main/resources/cert" ]; then
    echo "📤 上传证书文件..."
    scp -r kuangjia/src/main/resources/cert/* "$SERVER_HOST:$REMOTE_APP_DIR/cert/"
fi

# 6. 启动服务
echo "🚀 启动应用服务..."
ssh $SERVER_HOST "systemctl start $SERVICE_NAME"

# 7. 等待服务启动
echo "⏳ 等待应用启动..."
sleep 30

# 8. 检查服务状态
echo "🔍 检查服务状态..."
ssh $SERVER_HOST "
    systemctl status $SERVICE_NAME --no-pager | head -10
    
    echo ''
    echo '🔍 检查端口监听...'
    ss -tlnp | grep :8084 || echo '⚠️ 8084端口未监听'
    
    echo ''
    echo '🏥 检查健康状态...'
    sleep 10
    curl -s http://127.0.0.1:8084/game-api/actuator/health | head -5 || echo '⚠️ 健康检查失败'
    
    echo ''
    echo '📊 检查数据库连接(端口3366)...'
    ss -tlnp | grep :3366 || echo '⚠️ MySQL 3366端口未监听'
    
    echo ''
    echo '🔗 检查Redis连接...'
    ss -tlnp | grep :6379 || echo '⚠️ Redis 6379端口未监听'
"

# 9. 测试外网访问
echo ""
echo "🌐 测试外网HTTP访问..."
if curl -s http://piaosuanpan.com/game-api/actuator/health > /dev/null; then
    echo "✅ HTTP访问正常"
else
    echo "⚠️ HTTP访问可能有问题"
fi

echo ""
echo "🌐 测试外网HTTPS访问..."
if curl -k -s https://piaosuanpan.com/game-api/actuator/health > /dev/null; then
    echo "✅ HTTPS访问正常"
else
    echo "⚠️ HTTPS访问可能有问题 (可能需要配置SSL证书)"
fi

echo ""
echo "🎉 后端部署完成！"
echo ""
echo "📋 访问信息:"
echo "  - HTTP API接口:   http://piaosuanpan.com/game-api/"
echo "  - HTTPS API接口:  https://piaosuanpan.com/game-api/"
echo "  - 代练通前端:     http://piaosuanpan.com/dailiantong/"
echo "  - 原有ych项目:    http://piaosuanpan.com/"
echo "  - API文档:        http://piaosuanpan.com/game-api/swagger-ui.html"
echo "  - 健康检查:       http://piaosuanpan.com/game-api/actuator/health"
echo ""
echo "📊 服务状态检查命令:"
echo "  ssh $SERVER_HOST 'systemctl status $SERVICE_NAME'"
echo "  ssh $SERVER_HOST 'journalctl -u $SERVICE_NAME -f'"
echo "  ssh $SERVER_HOST 'tail -f /opt/dailiantong/logs/game-training-platform.log'"
echo ""
echo "🔧 其他有用命令:"
echo "  ssh $SERVER_HOST 'systemctl restart $SERVICE_NAME'  # 重启服务"
echo "  ssh $SERVER_HOST 'systemctl stop $SERVICE_NAME'     # 停止服务"
echo "  ssh $SERVER_HOST 'systemctl start $SERVICE_NAME'    # 启动服务"
echo ""